package com.lzc.springboot.service.impl;

import com.lzc.springboot.domain.ResultInfo;
import com.lzc.springboot.domain.ShowDisease;
import com.lzc.springboot.domain.TemporaryPicture;
import com.lzc.springboot.mapper.DiseaseMapper;
import com.lzc.springboot.mapper.TemporaryPicMapper;
import com.lzc.springboot.service.TemporaryPicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Service
public class TemporaryPicServiceImpl implements TemporaryPicService {

  @Autowired
  private TemporaryPicMapper temporaryPicMapper;

  @Autowired
  private DiseaseMapper diseaseMapper;

  @Override
  public ResultInfo<TemporaryPicture> save(TemporaryPicture picture) {
    temporaryPicMapper.save(picture);
    System.out.println(picture);
    ResultInfo<TemporaryPicture> resultInfo = new ResultInfo<>();
    resultInfo.setData(picture);
    resultInfo.setCode(1);
    resultInfo.setMsg("上传文件成功");
    return resultInfo;
  }

  @Override
  public void delete(TemporaryPicture picture) {

  }

  @Override
  public ShowDisease identify(int tempPicId) {
    Integer classNum = temporaryPicMapper.findClassById(tempPicId);
    ShowDisease showInfo;
    if (classNum == null){
      String result = "";
      String[] arg = new String[]{"D:\\Software\\Anaconda\\envs\\my_conda\\python.exe","D:\\My_Code\\springboot+vue\\crop2.0\\springboot\\src\\main\\resources\\pyFile\\model.py", String.valueOf(tempPicId)};
      try {
        Process proc = Runtime.getRuntime().exec(arg);
        BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        System.out.println(result);
        result = in.readLine();
        in.close();
        proc.waitFor();
      } catch (IOException | InterruptedException e) {
        e.printStackTrace();
      }
      showInfo = diseaseMapper.findShowInfo(Integer.parseInt(result)+1);
    }else{
      showInfo = diseaseMapper.findShowInfo(classNum);
    }
    showInfo.setId(tempPicId);
    System.out.println(showInfo);
    return showInfo;
  }
}
